package com.pengtu.web.app;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.pengtu.dao.Page;
import com.pengtu.dao.QueryFilter;
import com.pengtu.entity.app.Department;
import com.pengtu.entity.app.Organization;
import com.pengtu.entity.app.User;
import com.pengtu.security.SpringSecurityUtils;
import com.pengtu.service.ServiceException;
import com.pengtu.service.app.AccountManager;
import com.pengtu.utils.web.Struts2Utils;
import com.pengtu.web.BaseActionSupport;

/**
 * 部门管理
 * 
 * @author libitum
 * 
 */
@Namespace("/account")
@Results({
		@Result(name = BaseActionSupport.RELOAD, type = "redirect", location = "department.action?organizationId=${organizationId}") })
public class DepartmentAction extends BaseActionSupport<Department> {

	private static final long serialVersionUID = 1L;

	private AccountManager accountManager;

	// -- 页面属性 --//
	private String id;
	private Department entity;
	private List<Department> allDepartmentList;
	private List<Organization> organizationList;
	private Page<Department> page = new Page<Department>(10);// 每页5条记录
	private String organizationId;
	private String orgName;
	private String deptName;
	private Organization org;
	
	private User user = SpringSecurityUtils.getCurrentUser();


	@Action("account/department")
	@Override
	public String list() throws Exception {
		// TODO Auto-generated method stub
		QueryFilter filters = QueryFilter
				.buildFromHttpRequest(Struts2Utils.getRequest());
		filters.addFilter("organization.id",organizationId);
		
//		allDepartmentList = accountManager.getDepartment();
		// 设置默认排序方式
		
		if (!page.isOrderBySetted()) {
			page.setOrderBy("createDate");
			page.setOrder(Page.DESC);
		}
		page = accountManager.searchDepartment(page, filters);
		return SUCCESS;
	}

	@Action("/project/sign/org/department")
	@Override
	public String input() throws Exception {
		return INPUT;
	}

	@Override
	public String save() throws Exception {
		// TODO Auto-generated method stub
		org = accountManager.getOrganization(org.getId());
		if(null!=org)
		entity.setOrganization(org);
		accountManager.saveDepartment(entity);
		addActionMessage("保存部门成功");
		return RELOAD;
	}

	@Override
	public String delete() throws Exception {
		// TODO Auto-generated method stub
		try {
			accountManager.deleteDepartment(id);
			addActionMessage("删除部门成功");
		} catch (ServiceException e) {
			logger.error(e.getMessage(), e);
			addActionMessage("删除部门失败");
		}
		return RELOAD;
	}

	@Override
	protected void prepareModel() throws Exception {
		// TODO Auto-generated method stub
		if (StringUtils.isNotEmpty(id)) {
			entity = accountManager.getDepartment(id);
		} else {
			entity = new Department();
		}
	}

	@Action("/account/api/department")
	public String apilist() throws Exception {
		QueryFilter filters = QueryFilter
				.buildFromHttpRequest(Struts2Utils.getRequest());
		// 设置默认排序方式
		if ( filters == null) {
			allDepartmentList = accountManager.getDepartment();
		}
		if (!page.isOrderBySetted()) {
			page.setOrderBy("id");
			page.setOrder(Page.DESC);
		}
		filters.addFilter("organization.id", user.getOrganization().getId());
		page = accountManager.searchDepartment(page, filters);
		return SUCCESS;
	}

	public List<Department> getAllDepartmentList() {
		return allDepartmentList;
	}

	/**
	 * 支持使用Jquery.validate Ajax检验用户名是否重复.
	 */
	public String checkDepCode() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String newDepCode = request.getParameter("depCode");
		String oldDepCode = request.getParameter("oldDepCode");

		if (accountManager.isDepCodeUnique(newDepCode, oldDepCode)) {
			Struts2Utils.renderText("true");
		} else {
			Struts2Utils.renderText("false");
		}
		// 因为直接输出内容而不经过jsp,因此返回null.
		return null;
	}
	
	public String getDeptName() {
		return deptName;
	}

	public Organization getOrg() {
		return org;
	}

	public void setOrg(Organization org) {
		this.org = org;
	}

	public String getOrganizationId() {
		return organizationId;
	}

	public void setOrganizationId(String organizationId) {
		this.organizationId = organizationId;
	}

	public String getOrgName() {
		return orgName;
	}

	public void setOrgName(String orgName) {
		this.orgName = orgName;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public Department getEntity() {
		return entity;
	}

	public void setEntity(Department entity) {
		this.entity = entity;
	}

	public Page<Department> getPage() {
		return page;
	}

	public void setPage(Page<Department> page) {
		this.page = page;
	}

	public AccountManager getAccountManager() {
		return accountManager;
	}

	public void setAccountManager(AccountManager accountManager) {
		this.accountManager = accountManager;
	}

	public List<Organization> getOrganizationList() {
		return organizationList;
	}

	@Override
	public Department getModel() {
		// TODO Auto-generated method stub
		return entity;
	}

}
